package com.revenco.bi.service.impl;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.json.Json;
import org.nutz.lang.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;

import com.revenco.bi.dao.TnChartBaseinfoDao;
import com.revenco.bi.dao.TnChartDataitemsDao;
import com.revenco.bi.dao.TnChartParamsDao;
import com.revenco.bi.model.TnChartDataitems;
import com.revenco.bi.model.TnChartParams;
import com.revenco.bi.model.TnColumnInfo;
import com.revenco.bi.model.userbean.AnychartDataBean;
import com.revenco.bi.model.userbean.OilGaugeBean;
import com.revenco.bi.model.userbean.XFieldsBean;
import com.revenco.bi.model.userbean.YFieldsBean;
import com.revenco.bi.model.userbean.YQueryBean;
import com.revenco.bi.service.ChartService;
import com.revenco.bi.service.TnColumnInfoService;
import com.revenco.bi.util.ColorUtils;
import com.revenco.bi.util.JsonBinder;
import com.revenco.bi.util.XmlUtils;

/**
 * 图表操作的service层实现类
 * 
 * @author ghq
 * 
 */
@Service
public class ChartServiceImpl implements ChartService {
	@Autowired
	private TnChartBaseinfoDao chartBaseinfoDao;
	@Autowired 
	private TnChartDataitemsDao tDataitemsDao;
	@Autowired 
	private TnChartParamsDao tnParamsDao;
	@Autowired
	private TnColumnInfoService tInfoService;

	private JsonBinder binder;

	public Model getModel(Model model, String strJson,
			String xFields, String chartType, String seriesData, String yFields) {
		@SuppressWarnings("rawtypes")
		Map strJsonMap = new HashMap();
		strJsonMap = Json.fromJson(HashMap.class, Lang.inr(strJson));
		binder = new JsonBinder();
		XFieldsBean xField = binder.fromJson(xFields, XFieldsBean.class); // x轴数据

		// 以下为图形属性
		String chart_title = strJsonMap.get("chart_title").toString(); // 标题
		String title_enabled = strJsonMap.get("title_enabled").toString(); // 标题是否可用
		String axis_title_enabled = strJsonMap.get("axis_title_enabled") // x轴标题是否可用
				.toString();
		String title_font_size = strJsonMap.get("title_font_size").toString(); // 标题显示字体大小
		String axis_title_font_size = strJsonMap.get("axis_title_font_size") // x轴标题显示字体大小
				.toString();
		String animation = strJsonMap.get("animation").toString(); // 是否显示动画
		String legend_enabled = strJsonMap.get("legend_enabled").toString(); // 图例是否可用
		String legend_position = strJsonMap.get("legend_position").toString(); // 图例显示位置
		String legend_align = strJsonMap.get("legend_align").toString(); // 图例显示方式
		String y_axis_title = strJsonMap.get("y_axis_title").toString(); // y轴标题

		model.addAttribute("title_enabled", title_enabled);
		model.addAttribute("axis_title_enabled", axis_title_enabled);
		model.addAttribute("title_font_size", title_font_size);
		model.addAttribute("axis_title_font_size", axis_title_font_size);
		model.addAttribute("animation", animation);
		model.addAttribute("legend_enabled", legend_enabled);
		model.addAttribute("legend_position", legend_position);
		model.addAttribute("legend_align", legend_align);
		model.addAttribute("chartType", chartType);
		model.addAttribute("dataTemp", seriesData);
		model.addAttribute("title", chart_title);
		model.addAttribute("xTitle", xField.getcName());
		model.addAttribute("yTitle", y_axis_title);
		return model;
	}

	public String getSeriesData(Map<String, List<AnychartDataBean>> map,
			String yFields, String chartType) {
		binder = new JsonBinder();
		YQueryBean yAxis = binder.fromJson(yFields, YQueryBean.class);// y轴数据
		YFieldsBean[] yFieldsArray = yAxis.getFullSelect();

		StringBuilder data = new StringBuilder();
		String lengend = "";
		String SeriesName = "";
		String decimalDigits = "";
		String dataUnit = "";
		String[] types = chartType.split(",");
		String type = chartType;
		int length = types.length;
		XmlUtils xmlUtils = new XmlUtils();
		for (int i = 0; i < yFieldsArray.length; i++) {
			lengend = yFieldsArray[i].getName();
			SeriesName = yFieldsArray[i].getSeriesName();
			decimalDigits = yFieldsArray[i].getDecimalPrecision();
			dataUnit = yFieldsArray[i].getDataUnit();
			if (StringUtils.isEmpty(dataUnit)) {
				dataUnit = "";
			} else {
				dataUnit = "(" + dataUnit + ")";
			}
			if (StringUtils.isEmpty(decimalDigits)) {
				decimalDigits = String.valueOf(2);
			}
			if (length > 1) {
				if (i == 0) {
					type = "bar";
				} else {
					type = "line";
				}
			}
			data.append(xmlUtils.createXML(map.get(lengend), type, SeriesName, 
					decimalDigits, dataUnit));
		}
		return data.toString();
	}
	
	public TnChartDataitems getTnChartDataitemInfoById(String Id) {
		TnChartDataitems tnDataitems= tDataitemsDao.getChartDataitemsById(Id);
		return tnDataitems;
	}
	
	public TnChartParams getChartParamsByIdName(String chartId,String name) {
		TnChartParams tnChartParams= tnParamsDao.getChartParamsByIdName(chartId,name);
		return tnChartParams;
	}
	public TnChartParams getTnChartParamsInfoById(String paramId) {
		TnChartParams tParams = tnParamsDao.getChartParamsById(paramId);
		return tParams;
	}
	
	public OilGaugeBean getOilGaugeBeanInfo(String sql) {
		return chartBaseinfoDao.getOilGaugeBeanInfo(sql);
	}

	public Model getModelofOilGauge(Model model, String strJson,
			String xmlDataInfo, OilGaugeBean oBean, String yFields) {
		@SuppressWarnings("rawtypes")
		Map strJsonMap = new HashMap();
		strJsonMap = Json.fromJson(HashMap.class, Lang.inr(strJson));

		// 以下为图形属性
		String chart_title = strJsonMap.get("chart_title").toString(); // 标题
		String title_enabled = strJsonMap.get("title_enabled").toString(); // 标题是否可用
		String title_font_size = strJsonMap.get("title_font_size").toString(); // 标题显示字体大小
		String animation = strJsonMap.get("animation").toString(); // 是否显示动画
		double num = (Double.valueOf(oBean.getMaxCount() == null ? "0" : oBean.getMaxCount()) - Double
				.valueOf(oBean.getMinCount() == null ? "0" : oBean.getMinCount())) / 10;
		DecimalFormat df = new DecimalFormat("0");// 格式化小数
		String maxCount = df.format(Double.valueOf(oBean.getMaxCount() == null ? "0" : oBean.getMaxCount())); // 油量表最大刻度
		String minCount = df.format(Double.valueOf(oBean.getMinCount() == null ? "0" : oBean.getMinCount()) - num); // 油量表最小刻度

		model.addAttribute("title_enabled", title_enabled);
		model.addAttribute("title_font_size", title_font_size);
		model.addAttribute("animation", animation);
		model.addAttribute("title", chart_title);
		model.addAttribute("maxCount", maxCount);
		model.addAttribute("minCount", minCount);
		model.addAttribute("num", df.format(num));
		model.addAttribute("dataTemp", xmlDataInfo);

		return model;
	}

	public String getPointersInfo(Map<String, Map<String, String>> map,
			String yFields) {
		binder = new JsonBinder();
		YQueryBean yAxis = binder.fromJson(yFields, YQueryBean.class);// y轴数据
		YFieldsBean[] yFieldsArray = yAxis.getFullSelect();

		StringBuilder data = new StringBuilder();
		XmlUtils xmlUtils = new XmlUtils();
		ColorUtils colorUtils = new ColorUtils();
		TnColumnInfo tnInfo = new TnColumnInfo();
		String lengend = "";
		String tableId = "";
		int size = 4;
		String color = "";
		String decimalDigits = ""; 
		String dataUnit = "";
		
		for (int i = 0; i < yFieldsArray.length; i++) {
			lengend = yFieldsArray[i].getName();
			tableId = yFieldsArray[i].getTableId();
			tnInfo = tInfoService.getTNColumnInfoByName(tableId, lengend);
			decimalDigits = yFieldsArray[i].getDecimalPrecision();
			dataUnit = yFieldsArray[i].getDataUnit();
			color = colorUtils.getRandColorCode();
			if (StringUtils.isEmpty(dataUnit)) {
				dataUnit = "";
			} else {
				dataUnit = "(" + dataUnit + ")";
			}
			if (StringUtils.isEmpty(decimalDigits)) {
				decimalDigits = String.valueOf(2);
			}
			data.append(xmlUtils.createXML2(map.get(lengend),
					tnInfo.getcName(), size, color,
					decimalDigits, dataUnit));
			size += 5;
		}

		return data.toString();
	}
}
